<%--
  Created by IntelliJ IDEA.
  User: notyoukennk
  Date: 2022/11/20
  Time: 21:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page import="java.sql.*" %>
<%@ page import="ProjectManagement.DobConn" %>
<%@ page import="javax.swing.*" %>
<%@ page import="java.awt.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>修改留言</title>
  <style>
    body{
      margin: 0;
    }
    .change_box{
      height: 100%;
      width: 100%;
      background: linear-gradient(#5489d0, #ee61a8);
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .change_form{
      width: 40%;
      height: 30%;
      font-size: 2em;
      color: azure;
      background: #e66465;
      border-radius: 5px;
      margin: auto;
    }
    .content{
      border-radius: 25px;
      width: 300px;
      height: 50px;
      margin: 50px 0px 30px 0px;
      font-size: 0.8em;
    }
    .submit_box{
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .submit{
      width: 200px;
      height: 60px;
      background: #9198e5;
      border-radius: 35px;
      font-size: 1em;

    }
  </style>
</head>
<body>
<div class="change_box">
  <form class="change_form" action="messagechange.jsp" method="post">
    将留言修改为：<input type="text" name="content" class="content"><br>
    <div class="submit_box">
      <input type="submit" class="submit" value="修改">
    </div>
  </form>
</div>
<%
  try{
    request.setCharacterEncoding("UTF-8");
    String username = (String) session.getAttribute("username");
    String num = request.getParameter("num");
    String name = request.getParameter("name");

    if(request.getParameter("num")!=null&&request.getParameter("name") != null){
      application.setAttribute("num1",num);
      application.setAttribute("name1",name);
    }
    String num1 = (String) application.getAttribute("num1");
    String name1 = (String) application.getAttribute("name1");

    String new_content = request.getParameter("content");
    Connection conn = DobConn.getConn();
    Statement stat = conn.createStatement();
    if(name1.equals(username) && new_content != null){
      String sql = "UPDATE messages SET content='"+new_content+"' WHERE num ='"+num1+"';";
      stat.executeUpdate(sql);
//            out.print("<script>alert('修改成功！');</script>");
      Frame frame = new Frame();
      frame.setAlwaysOnTop(true);
      JOptionPane.showMessageDialog((Component) frame,"修改成功！");
      response.sendRedirect("messageboard.jsp");

    }else if(!name1.equals(name) && new_content != null){
      Frame frame = new Frame();
      frame.setAlwaysOnTop(true);
      JOptionPane.showMessageDialog((Component) frame,"不能修改其他人的留言！");
      response.sendRedirect("messageboard.jsp");
    }

  }catch (SQLException e){
    System.out.print(e);
  }

%>

</body>
</html>

